import os
import pathlib
import sys
import regex as re

from elasticsearch import Elasticsearch
project_path = str(pathlib.Path(os.path.dirname(__file__)).parent.parent.parent)
sys.path.append(project_path)
from shell.knowledge_graph.question_parser import QAParser

es = Elasticsearch()

import pandas as pd

raw = pd.read_excel((pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent /
                           'doc' /
                           '知识图谱问答系统开发计划.xlsx'), sheet_name="问题案例模板")
raw = raw[raw['是否聚合']!='是']


# data retrieval
top1_cnt=0
count=0
error_list= []
for i, each in raw.iterrows():
    node1_table_en=each['问题大类（节点）'].split('|')[1]
    node2_table_en=each['关系/节点'].split('|')[1]
    node2_attr_en=each["问题子类（属性）"].split('|')[1]
    node2_attr_cn=each["问题子类（属性）"].split('|')[0]
    raw_question = each['详细问题']
    to_replace=re.findall('\[.*\]',raw_question)
    question = raw_question.replace(to_replace[0],'')
    # print(question)
    parsers = QAParser()
    _, node2 = parsers.es_parser(question, es)

    if len(node2):
        node2_table = node2['table']
        node2_attr = node2['attr']
        node2_value = node2['value']
    else:
        node2_table= None
        node2_attr= None
        node2_value = None

    temp_dict = {'index':i,'node1_table': node1_table_en, 'node2_table': node2_table_en, 'node2_attr_en': node2_attr_en,
                  'node2_attr_cn': node2_attr_cn, 'question': question,'retrieved_cn':node2_value,'retrieved_en':node2_attr,'retrieved_table':node2_table}

    count += 1
    # print('node2:{}'.format(node2))
    if node2_table==node2_table_en and node2_attr==node2_attr_en and node2_value==node2_attr_cn:
        top1_cnt += 1
    else:
        error_list.append(temp_dict)


# 属性名抽取的准确率
top1_acc = top1_cnt/count
print(top1_acc)

result_path = (pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent /
                        'unittest' /
                        'shell' /
                        'knowledge_graph')

error_df = pd.DataFrame(error_list).to_excel(result_path / "error_case.xlsx",index=False)